import mongoose from 'mongoose';

const CandidateSchema = new mongoose.Schema({
  name: { type: String, required: true },
  signature: { type: String },
  imageUrl: { type: String },
  addedBy: { type: String },
  isCustom: { type: Boolean, default: false },
  addedAt: { type: Date, default: Date.now }
});

const VoteSchema = new mongoose.Schema({
  voteName: { type: String, required: true },
  creator: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
  inviteCode: { type: String, required: true, unique: true },
  candidates: [CandidateSchema],
  createdAt: { type: Date, default: Date.now },
  expireAt: { type: Date, required: true },
  chainVoteId: { type: Number, unique: true, sparse: true },
  maxVoters: { type: Number, required: true, default: 0 },
  currentVoters: { type: Number, default: 0 },
  expired: { type: Boolean, default: false },
  voterAddresses: {
    type: [String],
    default: []
  }
});

export default mongoose.model('Vote', VoteSchema);